import React, { Component } from 'react'
import { Redirect, Route } from 'react-router-dom'
import { isToken } from '../../utils/storage'

export default class AuthRoute extends Component {
  constructor(props) {
    super(props)
    this.state = {}
  }

  render() {
    const { component: Component, ...rest } = this.props
    return (
      <Route
        {...rest}
        render={(defaultProps) => {
          if (isToken()) {
            return <Component {...defaultProps}></Component>
          } else {
            return (
              <Redirect
                to={{
                  pathname: '/login',
                  state: { pathname: defaultProps.location.pathname }
                }}
              />
            )
          }
        }}></Route>
    )
  }
}
